--乘势连击
local chenshilianjie = fk.CreateSkill {
    name = "mcdh_chenshilianjie",
}

local U = require "packages/utility/utility"
local lib = require "packages/mcdh/lib"

chenshilianjie:addEffect(fk.CardUseFinished,{
    can_trigger = function(self, event, target, player, data)
        local room = player.room
        if player:hasSkill(self) and player == room.current and target == player and not table.find(player.room.alive_players, function(p) return p.dying end) then
            local events = room.logic:getEventsOfScope(GameEvent.UseCard, 999, function(e)
                local use = e.data[1]
                return use.from == player.id
            end, Player.HistoryTurn)
            self.cost_data = #events
            return #events <= player:getMark("mcdh_charge")
        end
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        lib.LoseCharge(player,player,self.cost_data,self.name)
        local flag = false
        if player:getMark("mcdh_charge") == 0 then flag = true end
        local cards = room:getNCards(self.cost_data)
        room:moveCards({
            ids = cards,
            toArea = Card.Processing,
            moveReason = fk.ReasonJustMove,
            skillName = self.name,
            proposer = player.id,
        })

        local redcard = table.filter(cards,function(id)return Fk:getCardById(id).color == Card.Red  end)
        local blackcard = table.filter(cards,function(id)return Fk:getCardById(id).color == Card.Black  end)
        if #redcard == 0 or #blackcard == 0 then
            local dummy = Fk:cloneCard("dilu")
            dummy:addSubcards(cards)
            room:obtainCard(player.id, dummy, true, fk.ReasonPrey)
        else
            local color = { "red", "black" }
            local choscolor = room:askForChoice(player, color, self.name)
            if choscolor == "red" then
                local dummy = Fk:cloneCard("dilu")
                dummy:addSubcards(redcard)
                room:obtainCard(player.id, dummy, true, fk.ReasonPrey)
                if flag then
                    local use =  U.askForUseVirtualCard(room,player,"slash",blackcard,self.name,"#chenshilianjie",true,true,false,false,nil,true)
                    if use then
                        room:useCard(use)
                    else
                        room:moveCards({
                            ids = blackcard,
                            toArea = Card.DiscardPile,
                            moveReason = fk.ReasonJustMove,
                            skillName = self.name,
                            proposer = player.id,
                        })
                    end
                end
            else
                local dummy = Fk:cloneCard("dilu")
                dummy:addSubcards(blackcard)
                room:obtainCard(player.id, dummy, true, fk.ReasonPrey)
                if flag then
                    local use = U.askForUseVirtualCard(room,player,"slash",redcard,self.name,"#chenshilianjie",true,true,false,false,nil,true)
                    if use then
                        room:useCard(use)
                    else
                        room:moveCards({
                            ids = redcard,
                            toArea = Card.DiscardPile,
                            moveReason = fk.ReasonJustMove,
                            skillName = self.name,
                            proposer = player.id,
                        })
                    end
                end
            end
        end
    end
})

Fk:loadTranslationTable {
    ["mcdh_chenshilianjie"] = "乘势连击",
    [":mcdh_chenshilianjie"] = "蓄力技(X),当你于你的回合使用第x张牌后，若没有角色处于濒死状态,你可以亮出牌堆顶X张牌,获得其中一种颜色的牌,若因此失去所有技力,你可以将剩余的亮出牌当做一张无次数限制的【杀】使用。",
    ["#chenshilianjie"] = "乘势连击:将剩余的亮出牌当做一张无次数限制的【杀】使用。",
}

return chenshilianjie